package com.ymatou.iapi.m.couponservice.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.QueryValidBatchListBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.ValidBatchInfo;
import com.ymatou.iapi.m.couponservice.service.QueryValidBatchListCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.utils.EnvSetup;
/**
*商家查询有效的优惠券列表用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryValidBatchList{
	private static QueryValidBatchListBean queryvalidbatchlistBean;
	private static QueryValidBatchListCall queryvalidbatchlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家查询有效的优惠券列表用例");
	}

	@Before
	public void caseUp() {
		queryvalidbatchlistBean=new QueryValidBatchListBean();
		queryvalidbatchlistCall=new QueryValidBatchListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryvalidbatchlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_001() {
		Logger.start(true,"商家查询有效的优惠券列表-根据SellerId，BatchCode，BatchName查询");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
		
			queryvalidbatchlistBean.setSellerId(batch.getiOperatorId());
			queryvalidbatchlistBean.setBatchCode(batch.getsBatchCode());
			queryvalidbatchlistBean.setBatchName(batch.getsCouponName());
			
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			VerifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_002() {
		Logger.start(true,"反例-商家查询有效的优惠券列表-setPageIndex=0,报错");
		try {
			queryvalidbatchlistBean.setSellerId(Integer.valueOf(EnvSetup.getData("SellerId")));
		
			queryvalidbatchlistBean.setPageSize(100);
			queryvalidbatchlistBean.setPageIndex(0);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_003() {
		Logger.start(true,"商家查询有效的优惠券列表-PageSize=0,报错");
		try {
			
			queryvalidbatchlistBean.setSellerId(Integer.valueOf(EnvSetup.getData("SellerId")));
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(0);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("3", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyIsContains("未知错误，系统异常",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_004() {
		Logger.start(true,"商家查询有效的优惠券列表-PageSize=1,返回一条数据");
		try {
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
		
			queryvalidbatchlistBean.setSellerId(batch.getiOperatorId());
			
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(1);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
			 Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
			 
			 List<ValidBatchInfo> list=queryvalidbatchlistCall.getBatchInfos().getBatchInfos();
			 Logger.verifyEquals(1,
					 list.size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_005() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空");
		try {

			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			couponServiceMCallService.CreateDefaultSendCouponBean(sellerCreateBatchBean.getSellerId(), sellerCreateBatchCall.getBatchCode());
			
			queryvalidbatchlistBean.setSellerId(sellerCreateBatchBean.getSellerId());
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);
			queryvalidbatchlistBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchInfos().getBatchInfos().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_006() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchName非空");
		try {
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			couponServiceMCallService.CreateDefaultSendCouponBean(sellerCreateBatchBean.getSellerId(), sellerCreateBatchCall.getBatchCode());
	
			queryvalidbatchlistBean.setSellerId(sellerCreateBatchBean.getSellerId());
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);
			queryvalidbatchlistBean.setBatchName(sellerCreateBatchBean.getBatchName());
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchInfos().getBatchInfos().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_007() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空,BatchName非空");
		try {
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			couponServiceMCallService.CreateDefaultSendCouponBean(sellerCreateBatchBean.getSellerId(), sellerCreateBatchCall.getBatchCode());
	
			queryvalidbatchlistBean.setSellerId(sellerCreateBatchBean.getSellerId());
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);

			queryvalidbatchlistBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			queryvalidbatchlistBean.setBatchName(sellerCreateBatchBean.getBatchName());
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 VerifyReturn();
			 
			 Logger.verifyEquals(1,
		        		queryvalidbatchlistCall.getBatchInfos().getBatchInfos().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_008() {
		Logger.start(true,"商家查询有效的优惠券列表-BatchCode非空,BatchName非空,BatchCode与BatchName不匹配");
		try {
			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
			
			couponServiceMCallService.CreateDefaultSendCouponBean(sellerCreateBatchBean.getSellerId(), sellerCreateBatchCall.getBatchCode());
	
			queryvalidbatchlistBean.setSellerId(sellerCreateBatchBean.getSellerId());
	
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);

			queryvalidbatchlistBean.setBatchCode(sellerCreateBatchCall.getBatchCode());
			queryvalidbatchlistBean.setBatchName(sellerCreateBatchBean.getBatchName()+"2222");
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
		     Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
			 Logger.verifyEquals(0,
		        		queryvalidbatchlistCall.getBatchInfos().getBatchInfos().size(), "验证返回条数");
		        
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_009() {
		Logger.start(true,"商家查询有效的优惠券列表-根据SellerId查询");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
		
			queryvalidbatchlistBean.setSellerId(batch.getiOperatorId());
			
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			VerifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryValidBatchList_010() {
		Logger.start(true,"商家查询有效的优惠券列表-根据SellerId查询-分享券不会出现");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			
			sellercreatebatchBean.setCouponType((byte)2);
			sellercreatebatchBean.setEffectiveDays(2);
		
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);
		
			queryvalidbatchlistBean.setSellerId(batch.getiOperatorId());
			
			queryvalidbatchlistBean.setPageIndex(1);
			queryvalidbatchlistBean.setPageSize(100);
			
			queryvalidbatchlistCall.setData(queryvalidbatchlistBean);
			queryvalidbatchlistCall.callService();			
			
			 Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
		                "验证返回code");
		     
		        Logger.verifyEquals("操作成功！",
		        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
		        
		        List<ValidBatchInfo> batchList = queryvalidbatchlistCall.getBatchInfos().getBatchInfos();
		        
		        for(int i=0;i<batchList.size();i++)
		        {
		        	 Logger.verifyNotEquals(sellerCreateBatchCall.getBatchCode(),
		        			 batchList.get(i).getBatchCode(), "验证分享券没有返回出来");
				      
		        }
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private void VerifyReturn() {
        Logger.verifyEquals("0", queryvalidbatchlistCall.getCode(),
                "验证返回code");
     
        Logger.verifyEquals("操作成功！",
        		queryvalidbatchlistCall.getMessage(), "验证返回Message");
        
        List<ValidBatchInfo> batchList = queryvalidbatchlistCall.getBatchInfos().getBatchInfos();
        
        for(int i=0;i<batchList.size();i++)
        {
        	 String batchCode = batchList.get(i).getBatchCode();
        	 ValidBatchInfo batchInfo = batchList.get(i);
        	 
        	 if(queryvalidbatchlistBean.getBatchCode()!=null)
        	 {
        		 Logger.verifyEquals(queryvalidbatchlistBean.getBatchCode(),
        				 batchInfo.getBatchCode(),
                          "验证返回记录中的BatchCode为传入BatchCode");
        	 }
        	 
        	 if(queryvalidbatchlistBean.getBatchName()!=null)
        	 {
        		 Logger.verifyEquals(queryvalidbatchlistBean.getBatchName(),
        				 batchInfo.getBatchName(),
                          "验证返回记录中的BatchCode为传入BatchCode");
        	 }
        	 
        	 Map item = new YmtCouponBatchWapper().selectBatchInfoBysBatchCode(batchCode).get(0);

        	 Logger.verifyEquals(queryvalidbatchlistBean.getSellerId(),
        			 Integer.parseInt(item.get("iOperatorId").toString()),
                      "验证返回记录中的iOperatorId为传入sellerid");
        
             Logger.verifyEquals(Integer.parseInt(item.get("iBatchId").toString()),
             		batchInfo.getBatchId(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中iBatchId字段相同");
             
             Logger.verifyEquals(item.get("sCouponName").toString(),
              		batchInfo.getBatchName(),
                      "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                              + "记录中sCouponName字段相同");
             
             Logger.verifyEquals(item.get("sBatchCode").toString(),
             		batchInfo.getBatchCode(),
                     "验证返回BatchCode与数据库中表Ymt_CouponBatch中sBatchCode=" + batchCode
                             + "记录中sBatchCode字段相同");

             Logger.verifyEquals(item.get("dValidStart").toString().subSequence(0, 19),
             		batchInfo.getEffectiveStartTime().replace("T", " "),
                     "验证返回getEffectiveStartTime与数据库中表Ymt_CouponSetting中dValidStart字段相同");

             Logger.verifyEquals(item.get("dValidEnd").toString().subSequence(0, 19),
             		batchInfo.getEffectiveEndTime().replace("T", " "),
                     "验证返回getEffectiveEndTime与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
             
           
             Logger.verifyEquals(Integer.parseInt(item.get("iCouponTotalNum").toString()),
             		batchInfo.getCouponTotalCount(),
                     "验证返回TotalCount与数据库中表Ymt_CouponBatch中iCouponTotalNum字段相同");
             
             Logger.verifyEquals(Integer.parseInt(item.get("iReceiveCount").toString()),
             		batchInfo.getReceivedCount(),
                     "验证返回BatchCode与数据库中表Ymt_CouponSetting中iReceiveCount字段相同");
             
             Logger.verifyEquals(0,
              		batchInfo.getOrderMinAmount().compareTo(new BigDecimal(item.get("fMinOrderValue").toString())),
                      "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");
             Logger.verifyEquals(0,batchInfo.getCouponAmount().compareTo(new BigDecimal(item.get("fCouponValue").toString())),
                "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");
        
             Logger.verifyEquals(Boolean.parseBoolean(item.get("IsShowInPage").toString()),
              		batchInfo.isShowOnPage(),
                      "验证返回BatchCode与数据库中表Ymt_CouponBatch中IsShowInPage字段相同");
              
        }

       
    }
}